using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace GameWP
{
    public class PlayUpdateComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game _game;
        private SpriteBatch _spriteBatch;
        private Vector2 touchPosition, pretouchPosition;
        private Vector2 _parallax;
        private MainGame _maingame;
        private float _scaleLW;
        private int _countWait, _maxCountWait;
        private Texture2D _win, _lose;
        public Vector2 Parallax
        {
            get { return _parallax; }
            set { _parallax = value; }
        }


        //Initilation
        public PlayUpdateComponent(Game game, MainGame maingame, SpriteBatch spriteBatch, Texture2D win, Texture2D lose)
            : base(game)
        {
            _game = game;
            _spriteBatch = spriteBatch;
            _maingame = maingame;
            Parallax = Vector2.One;

            //Init list menu item
            _win = win;
            _lose = lose;
            _scaleLW = 0.1f;
            _countWait = -1;
            _maxCountWait = 60;
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
                base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            // TODO: Add your update code here    
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                _game.Exit();
            //Key input
            TouchCollection touchCollection = TouchPanel.GetState();
            touchPosition = Vector2.Zero;
            if (touchCollection.Count > 0)
            {
                touchPosition = touchCollection[0].Position;
                pretouchPosition = touchPosition;
            }

            // Check attack
            // Luffy check with all zombie
            for (int i = 0; i < ManagerGame.managerZombie.ListZombiesNow.Count; i++)
            {
                if (ManagerGame.luffy.CheckAttack(ManagerGame.managerZombie.ListZombiesNow[i], gameTime)
                    && ManagerGame.luffy.StateL != StateL.Skill)
                    break;
            }

            // AHero now check with all zombie
            for (int i = 0; i < ManagerGame.managerAHero.ListAHeroNow.Count; i++)
            {
                if (ManagerGame.managerAHero.ListAHeroNow[i].IsDoctor)
                {
                    if (ManagerGame.managerAHero.ListAHeroNow[i].CheckAttack(ManagerGame.luffy, gameTime)) ;
                    else
                    {
                        for (int j = 0; j < ManagerGame.managerAHero.ListAHeroNow.Count; j++)
                            if (i != j)
                                if (ManagerGame.managerAHero.ListAHeroNow[i].CheckAttack(ManagerGame.managerAHero.ListAHeroNow[j], gameTime))
                                    break;
                    }
                    
                }
                else
                {
                    for (int j = 0; j < ManagerGame.managerZombie.ListZombiesNow.Count; j++)
                        if (ManagerGame.managerAHero.ListAHeroNow[i].CheckAttack(ManagerGame.managerZombie.ListZombiesNow[j], gameTime))
                            break;
                }
                
            }

            // All zombie checkk with: luffy, treasure, ahero
            for (int i = 0; i < ManagerGame.managerZombie.ListZombiesNow.Count; i++)
            {
                if (ManagerGame.managerZombie.ListZombiesNow[i].OnlyAttackTresure)
                    ManagerGame.managerZombie.ListZombiesNow[i].CheckAttack(ManagerGame.treasure, gameTime);
                else if (ManagerGame.managerZombie.ListZombiesNow[i].IsDoctor)
                {
                    for (int j = 0; j < ManagerGame.managerZombie.ListZombiesNow.Count; j++)
                        if (i != j && !ManagerGame.managerZombie.ListZombiesNow[j].IsDoctor)
                            if( ManagerGame.managerZombie.ListZombiesNow[i].CheckAttack(ManagerGame.managerZombie.ListZombiesNow[j], gameTime))
                                break;
                }
                else
                {
                    if (ManagerGame.managerZombie.ListZombiesNow[i].CheckAttack(ManagerGame.luffy, gameTime)) ;
                    else
                        if (ManagerGame.managerZombie.ListZombiesNow[i].CheckAttack(ManagerGame.treasure, gameTime)) ;
                        else
                        {
                            for (int j = 0; j < ManagerGame.managerAHero.ListAHeroNow.Count; j++)
                                if (ManagerGame.managerZombie.ListZombiesNow[i].CheckAttack(ManagerGame.managerAHero.ListAHeroNow[j], gameTime))
                                    break;
                        }
                }
            }

            //Check trap attack zombie
            for (int i = 0; i < ManagerGame.managerTrap.ListTrapActive.Count; i++)
            {
                for (int j = 0; j < ManagerGame.managerZombie.ListZombiesNow.Count; j++)
                    if (ManagerGame.managerTrap.ListTrapActive[i].CheckAttackWith(ManagerGame.managerZombie.ListZombiesNow[j], gameTime)) ;
            }
            
            ManagerGame.managerTrap.ManageUpdate(gameTime, touchPosition);
            ManagerGame.treasure.Update(gameTime, Vector2.Zero);
            ManagerGame.luffy.Update(gameTime, touchPosition);
            ManagerGame.managerAHero.ManageUpdate(gameTime, touchPosition);
            ManagerGame.managerZombie.ManageUpdate(gameTime);            

            if (ManagerGame.SoundOn)
                if (MediaPlayer.State == MediaState.Stopped || MediaPlayer.State == MediaState.Paused)
                    MediaPlayer.Play(ManagerGame.managerSound._gamePlay);

            //Kiem tra thua
            if (ManagerGame.treasure.State == State.Disappear || ManagerGame.luffy.StateL == StateL.Dead)
            {
                UpdateIntinsict();
                if (_countWait >= _maxCountWait)
                {
                    _countWait = -1;
                    _scaleLW = 0.1f;
                    ManagerGame.luffy.ResetLuffy(false);
                    if (ManagerGame.SoundOn)
                        MediaPlayer.Stop();
                    _maingame.ShowScreen(3);
                }
                
            }
            //Kiem tra thang
            if (ManagerGame.managerZombie.LevelWave >= ManagerGame.managerZombie.NumWave)
            {
                UpdateIntinsict();
                if (_countWait >= _maxCountWait)
                {
                    _countWait = -1;
                    _scaleLW = 0.1f;
                    ManagerGame.luffy.ResetLuffy(true);
                    if (ManagerGame.SoundOn)
                        MediaPlayer.Stop();
                    _maingame.ShowScreen(3);
                }                
            }
             
        }

        private void UpdateIntinsict()
        {
            _countWait++;
            _scaleLW += 0.1f;
            if (_scaleLW >= 2) _scaleLW = 2;
        }
        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();
            base.Draw(gameTime);
            if (_countWait > -1)
            {
                if (ManagerGame.managerZombie.LevelWave >= ManagerGame.managerZombie.NumWave)
                {
                    //Win
                    _spriteBatch.Draw(_win, new Vector2(200, 140), null, Color.White,0,Vector2.Zero,_scaleLW,SpriteEffects.None,0);
                }
                else
                {
                    //Lose
                    _spriteBatch.Draw(_lose, new Vector2(200, 140), null, Color.White, 0, Vector2.Zero, _scaleLW, SpriteEffects.None, 0);
                }
            }

            _spriteBatch.End();
        }
    }
}
